import {createStore, applyMiddleware, compose} from 'redux';
import thunk from 'redux-thunk';

import api from '../middleware/api';
import rootReducer from '../reducers';

const configureStore = preloadedState => {
    const store = createStore(
        rootReducer,
        preloadedState,
        compose(
            applyMiddleware(thunk, api)
        )
    );

    if (module.hot) {
        // Enable Webpack hot module replacement for reducers
        module.hot.accept('../reducers', () => {
            store.replaceReducer(rootReducer)
        })
    }

    return store;
};

module.exports = configureStore;
